package com.shuuro.widget.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.shuuro.NavigateMain;
import com.shuuro.R;
import com.shuuro.data.navigation.CommonNavigationGroup;
import com.shuuro.net.HttpConnectionUtil;
import com.shuuro.net.HttpConnectionUtil.HttpMethod;

public class NavigateListAdapter extends BaseExpandableListAdapter {

	private List<CommonNavigationGroup> commonNavigationGroups;
	private Context mContext;
	private TextView groupName;
	private GridView mGridView;
	
	private int mSelectedGroupPosition;
	
	public NavigateListAdapter(Context context, List<CommonNavigationGroup> listGroup){
		commonNavigationGroups = listGroup;
		mContext = context;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return commonNavigationGroups.get(groupPosition).getGroupItemsList().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return commonNavigationGroups.get(groupPosition).getGroupItemsList().get(childPosition).getItemId();
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(mContext).inflate(R.layout.navigation_group_item_view, null);
		mGridView = (GridView)convertView.findViewById(R.id.Navigation_group_item);
		mSelectedGroupPosition = groupPosition;
		mGridView.setAdapter(new CommonGridAdapter());
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return commonNavigationGroups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return commonNavigationGroups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(mContext).inflate(R.layout.navigation_group_view, null);
		groupName = (TextView)convertView.findViewById(R.id.navigation_group_name);
		groupName.setText(commonNavigationGroups.get(groupPosition).getName());
		groupName.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		groupName.setPadding(36, 0, 0, 0);
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public class CommonGridAdapter extends BaseAdapter{
		
		@Override
		public int getCount() {
			return commonNavigationGroups.get(mSelectedGroupPosition).getGroupItemsList().size();
		}
		
		@Override
		public Object getItem(int position) {
			return commonNavigationGroups.get(mSelectedGroupPosition).getGroupItemsList().get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = new TextView(mContext);
			((TextView)convertView).setGravity(Gravity.CENTER);
			String itemName = commonNavigationGroups.get(mSelectedGroupPosition)
								.getGroupItemsList().get(position).getItemName();
			SpannableString ss = new SpannableString(itemName);
			ss.setSpan(new UnderlineSpan(), 0, itemName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			
			((TextView)convertView).setText(ss);
			((TextView)convertView).setTag(R.id.group_id, mSelectedGroupPosition);
			((TextView)convertView).setTag(R.id.child_position, position);
			((TextView)convertView).setLayoutParams(new GridView.LayoutParams(30,30));
			((TextView)convertView).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					int groupId = (Integer)v.getTag(R.id.group_id);
					int position = (Integer)v.getTag(R.id.child_position);
					int itemId = commonNavigationGroups.get(groupId).getGroupItemsList().get(position).getItemId();
					HttpConnectionUtil http = new HttpConnectionUtil();
					
					Map<String, String> params = new HashMap<String, String>();
					params.put("id", "" + itemId);
					
					String url = "http://" + NavigateMain.DOMAIN + NavigateMain.URL_REQ_DATA;
					http.asyncConnect(url, params, HttpMethod.GET, new HttpConnectionUtil.HttpConnectionCallback() {
						
						@Override
						public void execute(String response) {
							Log.e("URL", response);
							Intent it = new Intent(Intent.ACTION_VIEW);
						    it.setData(Uri.parse(response));
						    it = Intent.createChooser(it, null);
						    mContext.startActivity(it);
						}
					});
				}
				
			});
			return convertView;
		}
		
	}
	
}
